const path = require('path')
const webpack = require('webpack')
const AddAssetHtmlPlugin = require('add-asset-html-webpack-plugin')
const CompressionPlugin = require('compression-webpack-plugin')
const TerserWebpackPlugin = require('terser-webpack-plugin')

const productionGZipExtensions = ['js', 'css', 'html', 'json']

const dllImport = (config, name) => {
  config.plugins.push(
    new webpack.DllReferencePlugin({
      context: __dirname,
      manifest: require(`./public/${name}/${name}-manifest.json`)
    })
  )
  config.plugins.push(
    new AddAssetHtmlPlugin({
      filepath: path.resolve(__dirname, `public/${name}/${name}.dll.js`),
      outputPath: name,
      publicPath: name
    })
  )
}

module.exports = {
  lintOnSave: false,
  devServer: {
    proxy: {
      '/boot': {
        // target: 'http://106.15.235.253:8080',
        target: 'http://localhost:8080',
        changeOrigin: true,
        proxyTimeout: 300000
      }
    },
    host: 'localhost',
    port: 8088
  },
  outputDir: './dist',
  assetsDir: 'public',
  productionSourceMap: false,
  publicPath: './',
  configureWebpack: config => {
    if (process.env.NODE_ENV === 'production') {
      dllImport(config, 'vue')
      config.plugins.push(
        new CompressionPlugin({
          test: new RegExp('\\.(' + productionGZipExtensions.join('|') + ')$'), //匹配文件名
          threshold: 10240,
          minRatio: 0.8,
          deleteOriginalAssets: false
        })
      )
      config.optimization.splitChunks.chunks = 'all'
      config.optimization.runtimeChunk = {
        name: entrypoint => `runtime-${entrypoint.name}`
      }
      config.optimization.minimizer.push(
        new TerserWebpackPlugin({
          terserOptions: {
            output: {
              comments: false
            }
          },
          extractComments: false
        })
      )
    }
  }
}
